package com.zhoug.router;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.alibaba.android.arouter.launcher.ARouter;
import com.zhoug.logging.Logger;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

/**
 * 页面导航路由器
 * @Author HK-LJJ
 * @Date 2020/3/19
 * @Description {@link Router}
 *
 */
@Deprecated
public class BaseARouter {
    private static final String TAG = ">>>Router";
    private static BaseARouter instance;

    /**
     * 单例
     *
     * @return
     */
    public static BaseARouter getInstance() {
        if (instance == null) {
            synchronized (BaseARouter.class) {
                if (instance == null) {
                    instance = new BaseARouter();
                }
            }
        }
        return instance;
    }

    private ARouter _getARouter() {
        return ARouter.getInstance();
    }


    public MyPostcard build(String path) {
        return new MyPostcard(_getARouter().build(path));
    }


    /**
     * 获取Fragment
     * @param path
     * @param bundle args
     * @return
     */
    public static Fragment createNewFragment(String path, Bundle bundle) {
        Object obj = getInstance().build(path).with(bundle).navigation();
        if (obj instanceof Fragment) {
            return (Fragment) obj;
        } else {
            return null;
        }
    }

    /**
     * 获取Fragment
     * @param path
     * @return
     */
    public static Fragment createNewFragment(String path) {
        return createNewFragment(path, null);
    }

    /**
     * 发现或者创建新的Fragment
     * @param fragmentManager
     * @param path 路由,Tag
     * @return
     */
    public static Fragment findOrCreateByTag(FragmentManager fragmentManager, String path){
        if(path==null){
            return null;
        }
        Fragment fragment = fragmentManager.findFragmentByTag(path);
        if(fragment==null){
            fragment=createNewFragment(path);
            Logger.d(TAG, "创建新的:"+fragment);
        }
        return fragment;
    }


    //>>>>>>>>>>>>>>>>>以下是原生封装>>>>>>>>>>>>>>>>>>>>>>>


    /**
     * 发现或者创建新的Fragment
     *
     * @param cls
     * @param fm
     * @param <T>
     * @return
     */

    public static <T extends Fragment> T findOrCreateByTag(FragmentManager fm, Class<T> cls) {
        return Router.findOrCreateByTag(fm, cls);
    }

    /**
     * 启动activity
     *
     * @param context
     * @param cls
     */
    public static void navigation(Context context, Class cls) {
        Router.navigation(context, cls);
    }


    /**
     * 启动activity
     * @param context
     * @param cls
     * @param bundle
     */
    public static void navigation(Context context, Class cls, Bundle bundle) {
        Router.navigation(context, cls, bundle);
    }

    /**
     * 启动activity
     * @param activity
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public static void navigation(Activity activity, Class cls, Bundle bundle, int requestCode) {
        Router.navigation(activity, cls, bundle,requestCode);
    }

    /**
     * 启动activity
     * @param activity
     * @param cls
     * @param requestCode
     */
    public static void navigation(Activity activity, Class cls, int requestCode) {
        Router.navigation(activity, cls, null,requestCode);
    }

    /**
     * 启动activity
     * @param fragment
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public static void navigation(Fragment fragment, Class cls, Bundle bundle,int requestCode) {
        Router.navigation(fragment, cls, bundle,requestCode);
    }

    /**
     * 启动activity
     * @param fragment
     * @param cls
     * @param requestCode
     */
    public static void navigation(Fragment fragment, Class cls, int requestCode) {
        Router.navigation(fragment, cls, null,requestCode);
    }

}
